package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.EarthQuery;
import com.ruoyi.system.domain.TheGeography;
import com.ruoyi.system.mapper.TheGeographyMapper;
import com.ruoyi.system.service.ITheGeographyService;
import com.ruoyi.system.utils.CSVReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * geographyService业务层处理
 *
 * @author ruoyi
 * @date 2023-04-17
 */
@Service
public class TheGeographyServiceImpl implements ITheGeographyService
{
    @Autowired
    private TheGeographyMapper theGeographyMapper;

    /**
     * 查询geography
     *
     * @param id geography主键
     * @return geography
     */
    @Override
    public TheGeography selectTheGeographyById(Long id)
    {
        return theGeographyMapper.selectTheGeographyById(id);
    }

    /**
     * 查询geography列表
     *
     * @param theGeography geography
     * @return geography
     */
    @Override
    public List<TheGeography> selectTheGeographyList(TheGeography theGeography)
    {
        return theGeographyMapper.selectTheGeographyList(theGeography);
    }

    /**
     * 新增geography
     *
     * @param theGeography geography
     * @return 结果
     */
    @Override
    public int insertTheGeography(TheGeography theGeography)
    {
        return theGeographyMapper.insertTheGeography(theGeography);
    }

    /**
     * 修改geography
     *
     * @param theGeography geography
     * @return 结果
     */
    @Override
    public int updateTheGeography(TheGeography theGeography)
    {
        return theGeographyMapper.updateTheGeography(theGeography);
    }

    /**
     * 批量删除geography
     *
     * @param ids 需要删除的geography主键
     * @return 结果
     */
    @Override
    public int deleteTheGeographyByIds(Long[] ids)
    {
        return theGeographyMapper.deleteTheGeographyByIds(ids);
    }

    /**
     * 删除geography信息
     *
     * @param id geography主键
     * @return 结果
     */
    @Override
    public int deleteTheGeographyById(Long id)
    {
        return theGeographyMapper.deleteTheGeographyById(id);
    }


    /**
     * 读取CSV文件，转换为TheGeography对象，并插入到数据库当中
     * @param filePath
     * @throws Exception
     */
    @Override
    public void readCsv(String filePath) throws Exception{
        CSVReader csvReader = new CSVReader(filePath);
        List<String[]> list = csvReader.read();
        // ["01/02/1965", "13:44:18", "19.246", "145.616", "Earthquake", "131.6","6"]
        if (!CollectionUtils.isEmpty(list)){
            TheGeography geography = new TheGeography();

            for (String[] s : list) {
                if (s[0].length()>10){
                    continue;
                }else {
                    int h = Integer.parseInt(s[1].split(":")[0]);
                    String f = s[1].split(":")[1];
                    String m = s[1].split(":")[2];
                    String hh = h>9?h+"":"0"+h;
                    String time = hh+":"+f+":"+m;
                    LocalDateTime dateTime = LocalDateTime.parse(LocalDate.parse(s[0], DateTimeFormatter.ofPattern("MM/dd/yyyy")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " " + time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    geography.setDateTime(dateTime);
                    geography.setLatitude(new BigDecimal(s[2]));
                    geography.setLongitude(new BigDecimal(s[3]));
                    geography.setType(s[4]);
                    geography.setDepth(new BigDecimal(s[5]));
                    geography.setMagnitude(new BigDecimal(s[6]));
                    theGeographyMapper.insertTheGeography(geography);
                }
            }
        }
    }

    /**
     * 柱状图一 按年获取地震信息
     * @param query
     * @return
     */
    @Override
    public List<List<String>>  getYear(EarthQuery query) {
        List<List<String>> resultList = new ArrayList<>();
        List<Map<String,String>> list = theGeographyMapper.getYear(query);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)){
            for (Map<String, String> map : list) {
                xAxis.add(map.get("year"));
                yAxis.add(map.get("count"));
            }
        }
        resultList.add(xAxis);
        resultList.add(yAxis);
        return resultList;
    }

    /**
     * 柱状图二 按月获取地震信息
     * @param query
     * @return
     */
    @Override
    public List<List<String>>  getMonth(EarthQuery query) {
        List<List<String>> resultList = new ArrayList<>();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        List<Map<String,String>> list = theGeographyMapper.getMonth(query);
        if (!CollectionUtils.isEmpty(list)){
            for (Map<String, String> map : list) {
                xAxis.add(map.get("month")+"月");
                yAxis.add(map.get("count"));
            }
        }
        resultList.add(xAxis);
        resultList.add(yAxis);
        return resultList;
    }

    /**
     * 柱状图三 按月获取地震信息
     * @param query
     * @return
     */
    @Override
    public List<List<String>> getDay(EarthQuery query) {
        List<List<String>> resultList = new ArrayList<>();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        List<Map<String,String>> list = theGeographyMapper.getDay(query);
        if (!CollectionUtils.isEmpty(list)){
            for (Map<String, String> map : list) {
                xAxis.add(map.get("date"));
                yAxis.add(map.get("count"));
            }
        }
        resultList.add(xAxis);
        resultList.add(yAxis);
        return resultList;
    }

    /**
     * 饼状图  获取地震的类型数据
     * @return
     */
    @Override
    public List<Map<String, String>> getType() {
        List<Map<String, String>> list = new ArrayList<>();
        list = theGeographyMapper.getType();
        return list;
    }

    /**
     * 波点图 获取震级和深度的关系
     * @return
     */
    @Override
    public List<Object []> getSet() {
        List<Object []> list = new ArrayList<>();
        List<Map<String,Object>> maps = theGeographyMapper.getSet();
        if (!CollectionUtils.isEmpty(maps)){
            for (Map<String, Object> map : maps) {
                Object [] arr = new Object[2];
                arr[0] = map.get("depth");
                arr[1] = map.get("magnitude");
                list.add(arr);
            }
        }
        return list;
    }

    /**
     * 根据 经纬度给数据赋值城市信息
     */
    @Override
    public void setCity() {
        List<TheGeography> list = theGeographyMapper.getChinaCity();
        if (!CollectionUtils.isEmpty(list)){
            for (TheGeography geography : list) {
                BigDecimal longitude = geography.getLongitude(); // 经度
                BigDecimal latitude = geography.getLatitude(); // 维度
                String city = getCity2(longitude,latitude);
                geography.setCity(city);
                theGeographyMapper.updateTheGeography(geography);
            }
        }
    }

    /**
     * 全国地震信息图
     * @param query
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCity(EarthQuery query) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<Map<String,String>> list = theGeographyMapper.selectCity(query);
        if (!CollectionUtils.isEmpty(list)){
            for (Map<String, String> map : list) {
                Map<String,Object> m = new HashMap<>();
                String[] values = map.get("value").split(";");
                m.put("name",map.get("name"));
                m.put("value",values);
                mapList.add(m);
            }
        }
        return mapList;
    }

    /**
     * 获取每个城市的地震发生数量
     * @param query
     * @return
     */
    @Override
    public List<List<String>> getCity(EarthQuery query) {
        List<List<String>> resultList = new ArrayList<>();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        List<Map<String,String>> list = theGeographyMapper.getCity(query);
        if (!CollectionUtils.isEmpty(list)){
            for (Map<String, String> map : list) {
                xAxis.add(map.get("city"));
                yAxis.add(map.get("count"));
            }
        }
        resultList.add(xAxis);
        resultList.add(yAxis);
        return resultList;
    }

    /**
     * 根据经纬度判断城市信息
     * @param longitude
     * @param latitude
     * @return
     */
    private String getCity2(BigDecimal longitude, BigDecimal latitude) {
        double jd = longitude.doubleValue();
        double wd = latitude.doubleValue();
        if ((jd>=121.183 && jd<=135.084) && (wd>=43.263 && wd<=53.55)){
            return "黑龙江";
        }else if ((jd>=121.633 && jd<=131.317) && (wd>=40.866 && wd<=46.3)){
            return "吉林";
        }else if ((jd>=118.883 && jd<=125.767) && (wd>=38.716 && wd<=43.433)){
            return "辽宁";
        }else if ((jd>=115.116 && jd<=117.067) && (wd>=39.066 && wd<=41.1)){
            return "北京";
        }else if ((jd>=116.7 && jd<=118.05) && (wd>=38.55 && wd<=40.25)){
            return "天津";
        }else if ((jd>=113.066 && jd<=119.883) && (wd>=36.016 && wd<=42.617)){
            return "河北";
        }else if ((jd>=114.316 && jd<=122.717) && (wd>=34.366 && wd<=38.383)){
            return "山东";
        }else if ((jd>=116.3 && jd<=121.95) && (wd>=30.75 && wd<=35.333)){
            return "江苏";
        }else if ((jd>=120.867 && jd<=131.317) && (wd>=40.866 && wd<=46.3)){
            return "上海";
        }else if ((jd>=121.633 && jd<=122.02) && (wd>=30.66 && wd<=31.883)){
            return "浙江";
        }else if ((jd>=115.833 && jd<=120.667) && (wd>=23.5 && wd<=28.55)){
            return "福建";
        }else if ((jd>=119.3 && jd<=124.567) && (wd>=20.75 && wd<=25.95)){
            return "台湾";
        }else if ((jd>=109.75 && jd<=117.333) && (wd>=20.15 && wd<=25.517)){
            return "广东";
        }else if ((jd>=108.25 && jd<=120.083) && (wd>=3.5 && wd<=20.116)){
            return "海南";
        }else if ((jd>=104.433 && jd<=112.067) && (wd>=20.9 && wd<=26.333)){
            return "广西";
        }else if ((jd>=113.576 && jd<=118.483) && (wd>=24.483 && wd<=30.078)){
            return "江西";
        }else if ((jd>=108.783 && jd<=114.25) && (wd>=24.633 && wd<=30.133)){
            return "湖南";
        }else if ((jd>=108.361 && jd<=116.130) && (wd>=29.031 && wd<=33.113)){
            return "湖北";
        }else if ((jd>=114.9 && jd<=119.617) && (wd>=24.683 && wd<=34.633)){
            return "安徽";
        }else if ((jd>=110.35 && jd<=116.65) && (wd>=31.383 && wd<=36.367)){
            return "河南";
        }else if ((jd>=111.5 && jd<=113.15) && (wd>=37.45 && wd<=38.417)){
            return "山西";
        }else if ((jd>=97.2 && jd<=126.067 ) && (wd>=37.4 && wd<=53.383)){
            return "内蒙古";
        }else if ((jd>=105.47 && jd<=111.27) && (wd>=31.7 && wd<=39.59)){
            return "陕西";
        }else if ((jd>=105.183 && jd<=110.183) && (wd>=28.166 && wd<=32.217)){
            return "重庆";
        }else if ((jd>=103.6 && jd<=109.583) && (wd>=24.616 && wd<=29.216)){
            return "贵州";
        }else if ((jd>=97.516 && jd<=106.183) && (wd>=21.133 && wd<=29.25)){
            return "云南";
        }else if ((jd>=97.35 && jd<=108.2) && (wd>=26.05 && wd<=34.317)){
            return "四川";
        }else if ((jd>=104.283 && jd<=109.65) && (wd>=35.233 && wd<=39.233)){
            return "宁夏";
        }else if ((jd>=92.216 && jd<=108.767) && (wd>=32.516 && wd<=42.95)){
            return "甘肃";
        }else if ((jd>=89.583 && jd<=103.067) && (wd>=31.15 && wd<=39.317)){
            return "青海";
        }else if ((jd>=73.667 && jd<=96.383) && (wd>=35.0 && wd<=50.0)){
            return "新疆";
        }else if ((jd>=78.416 && jd<=99.1) && (wd>=26.833 && wd<=36.883)){
            return "西藏";
        }else {
            return "其他地区";
        }

    }


}
